home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / utility1 / gs261src.zip / ZFONT2.C < prev    next >
C/C++ Source or Header  |  1993-05-27  |  14KB  |  427 lines

  1. /* Copyright (C) 1989, 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* zfont2.c */
  20. /* Font creation utilities for Ghostscript */
  21. #include "memory_.h"
  22. #include "ghost.h"
  23. #include "errors.h"
  24. #include "oper.h"
  25. #include "gxfixed.h"
  26. #include "gsmatrix.h"
  27. #include "gxdevice.h"
  28. #include "gschar.h"
  29. #include "gxfont.h"
  30. #include "alloc.h"
  31. #include "bfont.h"
  32. #include "dict.h"
  33. #include "dparam.h"
  34. #include "ilevel.h"
  35. #include "iname.h"
  36. #include "packed.h"
  37. #include "save.h"        /* for alloc_array */
  38. #include "store.h"
  39.  
  40. /* Global font-related objects */
  41. /* Names of system-known keys in font dictionaries: */
  42. static ref name_FontType;
  43. static ref name_FontName;
  44. static ref name_WMode;
  45. static ref name_Encoding;
  46. static ref name_FontBBox;
  47. static ref name_UniqueID;
  48. static ref name_XUID;
  49. static ref name_BuildChar;
  50. static ref name_BuildGlyph;
  51. /* Bitmap fonts */
  52. static ref name_BitmapWidths;
  53. static ref name_ExactSize;
  54. static ref name_InBetweenSize;
  55. static ref name_TransformedChar;
  56.  
  57. /* Registered encodings.  See font.h for documentation. */
  58. ref registered_Encodings[registered_Encodings_countof];
  59.  
  60. /* Initialize the font building operators */
  61. private void
  62. zfont2_init(void)
  63. {    static const names_def fnd2[] = {
  64.  
  65.     /* Create the names of the standard elements of */
  66.     /* a font dictionary. */
  67.        { "FontType", &name_FontType },
  68.        { "FontName", &name_FontName },
  69.        { "WMode", &name_WMode },
  70.        { "Encoding", &name_Encoding },
  71.        { "FontBBox", &name_FontBBox },
  72.        { "UniqueID", &name_UniqueID },
  73.        { "XUID", &name_XUID },
  74.        { "BuildChar", &name_BuildChar },
  75.        { "BuildGlyph", &name_BuildGlyph },
  76.        { "BitmapWidths", &name_BitmapWidths },
  77.        { "ExactSize", &name_ExactSize },
  78.        { "InBetweenSize", &name_InBetweenSize },
  79.        { "TransformedChar", &name_TransformedChar },
  80.  
  81.     /* Mark the end of the initialized name list. */
  82.        names_def_end
  83.     };
  84.  
  85.     init_names(fnd2);
  86.  
  87.     /* Initialize the registered Encodings. */
  88.     {    int i;
  89.         for ( i = 0; i < registered_Encodings_countof; i++ )
  90.             make_array(®istered_Encodings[i], 0, 0, NULL);
  91.     }
  92. }
  93.  
  94. /* <string|name> <font_dict> .buildfont3 <string|name> <font> */
  95. /* Build a type 3 (user-defined) font. */
  96. int
  97. zbuildfont3(os_ptr op)
  98. {    int ccode, gcode, code;
  99.     build_proc_refs build;
  100.     ref pnull;
  101.     gs_font *pfont;
  102.     check_type(*op, t_dictionary);
  103.     ccode = dict_find(op, &name_BuildChar, &build.pBuildChar);
  104.     gcode = dict_find(op, &name_BuildGlyph, &build.pBuildGlyph);
  105.     make_null(&pnull);
  106.     if ( ccode <= 0 )
  107.     {    if ( gcode <= 0 )
  108.             return_error(e_invalidfont);
  109.         build.pBuildChar = &pnull;
  110.     }
  111.     else
  112.     {    check_proc(*build.pBuildChar);
  113.     }
  114.     if ( gcode <= 0 )
  115.         build.pBuildGlyph = &pnull;
  116.     else
  117.     {    check_proc(*build.pBuildGlyph);
  118.     }
  119.     code = build_gs_simple_font(op, &pfont, ft_user_defined, &build);
  120.     if ( code < 0 )
  121.         return code;
  122.     return define_gs_font(pfont);
  123. }
  124.  
  125. /* <int> <packedarray> .registerencoding - */
  126. private int
  127. zregisterencoding(register os_ptr op)
  128. {    long i;
  129.     check_type(op[-1], t_integer);
  130.     check_read_type(*op, t_shortarray);
  131.     i = op[-1].value.intval;
  132.     if ( i >= 0 && i < registered_Encodings_countof )
  133.     {    ref_assign_old(®istered_Encodings[i], op,
  134.                    ".registerencoding");
  135.     }
  136.     pop(2);
  137.     return 0;
  138. }
  139.  
  140. /* Encode a character. */
  141. /* (This is very inefficient right now; we can speed it up later.) */
  142. private gs_glyph
  143. zfont_encode_char(gs_show_enum *penum, gs_font *pfont, gs_char *pchr)
  144. {    const ref *pencoding =
  145.         &((font_data *)(pfont->client_data))->Encoding;
  146.     ulong index = *pchr;    /* work around VAX widening bug */
  147.     ref cname;
  148.     int code = array_get(pencoding, (long)index, &cname);
  149.     if ( code < 0 || !r_has_type(&cname, t_name) )
  150.         return gs_no_glyph;
  151.     return (gs_glyph)name_index(&cname);
  152. }
  153.  
  154. /* Get the name of a glyph. */
  155. /* The following typedef is needed to work around a bug in */
  156. /* some AIX C compiler. */
  157. typedef const char *const_chars;
  158. private const_chars
  159. zfont_glyph_name(gs_glyph index, uint *plen)
  160. {    ref nref, sref;
  161.     name_index_ref(index, &nref);
  162.     name_string_ref(&nref, &sref);
  163.     *plen = r_size(&sref);
  164.     return (const char *)sref.value.const_bytes;
  165. }
  166.  
  167. /* ------ Initialization procedure ------ */
  168.  
  169. op_def zfont2_op_defs[] = {
  170.     {"2.buildfont3", zbuildfont3},
  171.     {"2.registerencoding", zregisterencoding},
  172.     op_def_end(zfont2_init)
  173. };
  174.  
  175. /* ------ Subroutines ------ */
  176.  
  177. /* Do the common work for building a font of any non-composite FontType. */
  178. /* The caller guarantees that *op is a dictionary. */
  179. int
  180. build_gs_simple_font(os_ptr op, gs_font **ppfont, font_type ftype,
  181.   const build_proc_refs *pbuild)
  182. {    ref *pbbox;
  183.     float bbox[4];
  184.     gs_uid uid;
  185.     int code;
  186.     gs_font *pfont;
  187.     /* Pre-clear the bbox in case it's invalid. */
  188.     /* The Red Books say that FontBBox is required, */
  189.     /* but the Adobe interpreters don't require it, */
  190.     /* and a few user-written fonts don't supply it, */
  191.     /* or supply one of the wrong size (!). */
  192.     bbox[0] = bbox[1] = bbox[2] = bbox[3] = 0.0;
  193.     if ( dict_find(op, &name_FontBBox, &pbbox) > 0 )
  194.     {    if ( !r_is_array(pbbox) )
  195.             return_error(e_invalidfont);
  196.         if ( r_size(pbbox) == 4 )
  197.         {    const ref_packed *pbe = pbbox->value.packed;
  198.             ref rbe[4];
  199.             int i;
  200.             for ( i = 0; i < 4; i++ )
  201.             {    packed_get(pbe, rbe + i);
  202.                 pbe = packed_next(pbe);
  203.             }
  204.             if ( num_params(rbe + 3, 4, bbox) < 0 )
  205.                 return_error(e_invalidfont);
  206.         }
  207.     }
  208.     code = get_gs_font_uid(op, &uid);
  209.     if ( code < 0 ) return code;
  210.     code = build_gs_font(op, ppfont, ftype, pbuild);
  211.     if ( code != 0 ) return code;    /* invalid or scaled font */
  212.     pfont = *ppfont;
  213.     pfont->data.base.FontBBox.p.x = bbox[0];
  214.     pfont->data.base.FontBBox.p.y = bbox[1];
  215.     pfont->data.base.FontBBox.q.x = bbox[2];
  216.     pfont->data.base.FontBBox.q.y = bbox[3];
  217.     pfont->data.base.UID = uid;
  218.     {    const ref *pfe =
  219.             &((font_data *)(pfont->client_data))->Encoding;
  220.         int index;
  221.         for ( index = registered_Encodings_countof; --index >= 0; )
  222.           if ( obj_eq(pfe, ®istered_Encodings[index]) )
  223.             break;
  224.         pfont->data.base.encoding_index = index;
  225.         if ( index < 0 )
  226.         {    /* Look for an encoding that's "close". */
  227.             int near_index = -1;
  228.             uint esize = r_size(pfe);
  229.             uint best = esize / 3;    /* must match at least this many */
  230.             for ( index = registered_Encodings_countof; --index >= 0; )
  231.             {    const ref *pre = ®istered_Encodings[index];
  232.                 uint match = esize;
  233.                 int i;
  234.                 if ( r_size(pre) != esize )
  235.                     continue;
  236.                 for ( i = esize; --i >= 0; )
  237.                 {    ref fe, re;
  238.                     array_get(pfe, (long)i, &fe);
  239.                     array_get(pre, (long)i, &re);
  240.                     if ( !obj_eq(&fe, &re) )
  241.                         match--;
  242.                 }
  243.                 if ( match > best )
  244.                     best = match,
  245.                     near_index = index;
  246.             }
  247.             index = near_index;
  248.         }
  249.         pfont->data.base.nearest_encoding_index = index;
  250.     }
  251.     return 0;
  252. }
  253.  
  254. /* Get the UniqueID or XUID from a font dictionary. */
  255. int
  256. get_gs_font_uid(os_ptr op, gs_uid *puid)
  257. {    ref *puniqueid;
  258.     /* In a Level 2 environment, check for XUID first. */
  259.     if ( level2_enabled && dict_find(op, &name_XUID, &puniqueid) > 0 )
  260.     {    long *xvalues;
  261.         uint size, i;
  262.         check_array_else(*puniqueid, e_invalidfont);
  263.         size = r_size(puniqueid);
  264.         if ( size == 0 )
  265.             return_error(e_invalidfont);
  266.         xvalues = (long *)alloc(size, sizeof(long), "get XUID");
  267.         if ( xvalues == 0 )
  268.             return_error(e_VMerror);
  269.         /* Get the values from the XUID array. */
  270.         for ( i = 0; i < size; i++ )
  271.         {    const ref *pvalue = puniqueid->value.const_refs + i;
  272.             if ( !r_has_type(pvalue, t_integer) )
  273.             {    alloc_free((char *)xvalues, size, sizeof(long), "get XUID");
  274.                 return_error(e_invalidfont);
  275.             }
  276.             xvalues[i] = pvalue->value.intval;
  277.         }
  278.         uid_set_XUID(puid, xvalues, size);
  279.         return 0;
  280.     }
  281.     /* If no UniqueID entry, set the UID to invalid, */
  282.     /* because UniqueID need not be present in all fonts, */
  283.     /* and if it is, the legal range is 0 to 2^24-1. */
  284.     if ( dict_find(op, &name_UniqueID, &puniqueid) <= 0 )
  285.         uid_set_invalid(puid);
  286.     else
  287.        {    if ( !r_has_type(puniqueid, t_integer) ||
  288.              puniqueid->value.intval < 0 ||
  289.              puniqueid->value.intval > ((1L << 24) - 1)
  290.            )
  291.             return_error(e_invalidfont);
  292.         /* Apparently fonts created by Fontographer often have */
  293.         /* a UniqueID of 0, contrary to Adobe's specifications. */
  294.         /* Treat 0 as equivalent to -1 (no UniqueID). */
  295.         if ( puniqueid->value.intval == 0 )
  296.             uid_set_invalid(puid);
  297.         else
  298.             uid_set_UniqueID(puid, puniqueid->value.intval);
  299.        }
  300.     return 0;
  301. }
  302.  
  303. /* Do the common work for building a font of any FontType. */
  304. /* The caller guarantees that *op is a dictionary. */
  305. /* op[-1] must be the key under which the font is being registered */
  306. /* in FontDirectory, normally a name or string. */
  307. /* Return 0 for a new font, 1 for a font made by makefont or scalefont, */
  308. /* or a negative error code. */
  309. private void get_font_name(P2(ref *, const ref *));
  310. private void copy_font_name(P2(gs_font_name *, const ref *));
  311. int
  312. build_gs_font(os_ptr op, gs_font **ppfont, font_type ftype,
  313.   const build_proc_refs *pbuild)
  314. {    ref kname, fname;        /* t_string */
  315.     ref *pftype;
  316.     ref *pfontname;
  317.     ref *pmatrix;
  318.     gs_matrix mat;
  319.     ref *pencoding;
  320.     int bitmapwidths, exactsize, inbetweensize, transformedchar;
  321.     int wmode;
  322.     int code;
  323.     gs_font *pfont;
  324.     ref *pfid;
  325.     ref *aop = dict_access_ref(op);
  326.     get_font_name(&kname, op - 1);
  327.     if ( dict_find(op, &name_FontType, &pftype) <= 0 ||
  328.         !r_has_type(pftype, t_integer) ||
  329.         pftype->value.intval != (int)ftype ||
  330.         dict_find(op, &name_FontMatrix, &pmatrix) <= 0 ||
  331.         read_matrix(pmatrix, &mat) < 0 ||
  332.         dict_find(op, &name_Encoding, &pencoding) <= 0 ||
  333.         !r_is_array(pencoding)
  334.        )
  335.         return_error(e_invalidfont);
  336.     if ( dict_find(op, &name_FontName, &pfontname) > 0 )
  337.         get_font_name(&fname, pfontname);
  338.     else
  339.         make_string(&fname, a_readonly, 0, NULL);
  340.     if ( (code = dict_int_param(op, &name_WMode, 0, 1, 0, &wmode)) < 0 ||
  341.          (code = dict_bool_param(op, &name_BitmapWidths, 0, &bitmapwidths)) < 0 ||
  342.          (code = dict_int_param(op, &name_ExactSize, 0, 2, fbit_use_bitmaps, &exactsize)) < 0 ||
  343.          (code = dict_int_param(op, &name_InBetweenSize, 0, 2, fbit_use_outlines, &inbetweensize)) < 0 ||
  344.          (code = dict_int_param(op, &name_TransformedChar, 0, 2, fbit_use_outlines, &transformedchar)) < 0
  345.        )
  346.         return code;
  347.     code = dict_find(op, &name_FID, &pfid);
  348.     if ( r_has_attr(aop, a_write) )
  349.        {    /* Assume this is a new font */
  350.         ref daref;
  351.         font_data *pdata;
  352.         if ( code > 0 )
  353.             return_error(e_invalidfont);    /* has FID already */
  354.         if ( (pfont = (gs_font *)alloc(1, sizeof(gs_font), "buildfont(font)")) == 0 )
  355.             return_error(e_VMerror);
  356.         code = alloc_array(&daref, 0, sizeof(font_data) / sizeof(ref), "buildfont(data)");
  357.         if ( code < 0 )
  358.             return code;
  359.         if ( (code = add_FID(op, pfont)) < 0 )
  360.             return code;
  361.         pdata = (font_data *)daref.value.refs;
  362.         ref_assign_new(&pdata->dict, op);
  363.         ref_assign_new(&pdata->BuildChar, pbuild->pBuildChar);
  364.         ref_assign_new(&pdata->BuildGlyph, pbuild->pBuildGlyph);
  365.         ref_assign_new(&pdata->Encoding, pencoding);
  366.         pfont->base = pfont;
  367.         pfont->dir = ifont_dir;
  368.         pfont->client_data = (char *)pdata;
  369.         pfont->FontType = ftype;
  370.         pfont->FontMatrix = mat;
  371.         pfont->BitmapWidths = bitmapwidths;
  372.         pfont->ExactSize = exactsize;
  373.         pfont->InBetweenSize = inbetweensize;
  374.         pfont->TransformedChar = transformedchar;
  375.         pfont->WMode = wmode;
  376.         pfont->build_char_proc = gs_no_build_char_proc;
  377.         pfont->encode_char_proc = zfont_encode_char;
  378.         pfont->glyph_name_proc = zfont_glyph_name;
  379.        }
  380.     else
  381.        {    /* Assume this was made by makefont or scalefont */
  382.         if ( code <= 0 || !r_has_type(pfid, t_fontID) )
  383.             return_error(e_invalidfont);
  384.         pfont = pfid->value.pfont;
  385.        }
  386.     copy_font_name(&pfont->key_name, &kname);
  387.     copy_font_name(&pfont->font_name, &fname);
  388.     *ppfont = pfont;
  389.     return 0;
  390. }
  391.  
  392. /* Get the string corresponding to a font name. */
  393. /* If the font name isn't a name or a string, return an empty string. */
  394. private void
  395. get_font_name(ref *pfname, const ref *op)
  396. {    switch ( r_type(op) )
  397.     {
  398.     case t_string:
  399.         *pfname = *op;
  400.         break;
  401.     case t_name:
  402.         name_string_ref(op, pfname);
  403.         break;
  404.     default:
  405.         /* This is weird, but legal.... */
  406.         make_string(pfname, a_readonly, 0, NULL);
  407.     }
  408. }
  409.  
  410. /* Copy a font name into the gs_font structure. */
  411. private void
  412. copy_font_name(gs_font_name *pfstr, const ref *pfname)
  413. {    uint size = r_size(pfname);
  414.     if ( size > gs_font_name_max )
  415.         size = gs_font_name_max;
  416.     memcpy(&pfstr->chars[0], pfname->value.const_bytes, size);
  417.     pfstr->size = size;
  418. }
  419.  
  420. /* Finish building a font, by calling gs_definefont if needed. */
  421. int
  422. define_gs_font(gs_font *pfont)
  423. {    return (pfont->base == pfont ?        /* i.e., original font */
  424.         gs_definefont(ifont_dir, pfont) :
  425.         0);
  426. }
  427.